package org.water.garden.activity.contact;

import org.water.garden.R;
import org.water.garden.core.ImUtils;
import org.water.garden.core.Intents;
import org.water.garden.view.ContactListAdapter;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Im.Account;
import android.provider.Im.Chats;
import android.provider.Im.Contacts;
import android.view.Menu;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author sshwsfc@gmail.com
 * 
 * Display All contact for a account
 */
public class ContactList extends ListActivity {

	public static final int DELETE_ID = Menu.FIRST;
	
	private Uri url;
	private Cursor contactCursor;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setDefaultKeyMode(SHORTCUT_DEFAULT_KEYS);

		Cursor cursor = ImUtils.queryAccount(getContentResolver(), getIntent()
				.getData(), new String[] { Account._ID, Account.PROVIDER, Account.NAME });
		
		if(cursor.first()){
			contactCursor = Contacts.queryContactsBy(getContentResolver(), new String[]{
				Contacts._ID, Contacts.NICKNAME, Contacts.USERNAME, "mode"
			}, cursor.getInt(1), cursor.getInt(0), null, null);
			cursor.close();
			
			ListAdapter adapter = new ContactListAdapter(this, contactCursor);
			
			setListAdapter(adapter);
		} else {
			Toast.makeText(this, "ERROR Content Data", 300).show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		Intent intent = new Intent(null, Contacts.CONTENT_URI);
		intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
		menu.addIntentOptions(Menu.ALTERNATIVE, 0, new ComponentName(this, ContactList.class), null, intent, 0, null);
		menu.add(Menu.SELECTED_ALTERNATIVE, DELETE_ID, R.string.contact_refresh, R.drawable.account_remove).setShortcut('2', 'd');

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		final boolean haveItems = contactCursor.count() > 0;

		if (!haveItems) {
			menu.removeGroup(Menu.SELECTED_ALTERNATIVE);
		}

		menu.setItemShown(DELETE_ID, haveItems);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		switch (item.getId()) {
		case DELETE_ID:
			getContentResolver().delete(Contacts.CONTENT_URI, null, null);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		url = ContentUris.withAppendedId(Contacts.CONTENT_URI, id);

		String action = getIntent().getAction();
		if (Intent.PICK_ACTION.equals(action) || Intent.GET_CONTENT_ACTION.equals(action)) {
			setResult(RESULT_OK, url.toString());
		} else {
			Cursor c = getContentResolver().query(Chats.CONTENT_URI, new String[]{Chats._ID}, Chats.CONTACT_ID + "=?", 
					new String[]{Long.toString(id)}, null);
			Uri chatUri = Chats.CONTENT_URI;
			if(c.first()){
				chatUri = ContentUris.withAppendedId(Chats.CONTENT_URI, c.getInt(0));
			}else{
				ContentValues values = new ContentValues();
				
				values.put(Chats.CONTACT_ID, id);
				values.put(Chats.GROUP_CHAT, 0);
				
				chatUri = getContentResolver().insert(Chats.CONTENT_URI, values);
			}
			
			Intent viewChatInt = new Intent(Intents.VIEW_CHAT, chatUri);
			startActivity(viewChatInt);
		}
	}
	
}
